package com.wchat.controller;

import com.wchat.converter.OrderFrom2OrderDtoConverter;
import com.wchat.dataobject.OrderDetail;
import com.wchat.dto.CartDto;
import com.wchat.dto.OrderDto;
import com.wchat.enums.ResultEnum;
import com.wchat.exceptions.SellException;
import com.wchat.form.OrderForm;
import com.wchat.repository.OrderDetailRepository;
import com.wchat.service.BuyerService;
import com.wchat.service.OrderService;
import com.wchat.utils.ResultVoUtils;
import com.wchat.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangtianrui on 17/8/12.
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderContronller {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailRepository orderDetailRepository;
    @Autowired
    private BuyerService buyerService;

    /**
     * 创建订单
     * @param orderForm 下单情况
     * @param bindingResult 表单绑定
     * @return ResultVo
     */
    @PostMapping("/create")
    private ResultVo<Map<String ,String>> create(@Valid OrderForm orderForm,
                                                 BindingResult bindingResult)
    {
        if(bindingResult.hasErrors())
        {
            log.error("[创建订单] 参数不正确{}",orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDto orderDto = OrderFrom2OrderDtoConverter.converter(orderForm);
        if(CollectionUtils.isEmpty(orderDto.getOrderDetails()))
        {
            log.error("[创建订单] 错误orderDto{}",orderDto);
            throw new SellException(ResultEnum.CART_IS_EMPTY);
        }
        //  创建订单
        OrderDto result = orderService.create(orderDto);
        //  返回信息
        Map<String ,String> map = new HashMap<>();
        map.put("orderId",result.getOrderId());
        return ResultVoUtils.success(map);
    }
    @GetMapping("/list")
    public ResultVo<List<OrderDto>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "pageNum",defaultValue = "0") Integer pageNum,
                                         @RequestParam(value = "size",defaultValue = "10") Integer pageSize)
    {
        if(StringUtils.isEmpty(openid))
        {
            log.error("[订单列表] openid错误{}",pageNum);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest pageRequest = new PageRequest(pageNum,pageSize);
        Page<OrderDto> orderDtoPage =  orderService.findOrderList(openid,pageRequest);

        return  ResultVoUtils.success(orderDtoPage.getContent());
    }
    /**
     * 订单详情
     * @param openid 用户openid
     * @param orderid 订单id
     * @return 返回结果VO
     */
    @GetMapping("/detail")
    public ResultVo<OrderDto> orderDetail(@RequestParam(value = "openid") String openid,
                                          @RequestParam("orderid")String orderid)
    {
        OrderDto orderDto = buyerService.findOrderOne(orderid,openid);
        return ResultVoUtils.success(orderDto);
    }
    /**
     *  取消订单
     * @param openid openid
     * @param orderid 订单id
     * @return
     */
    @PostMapping("/cancel")
    public ResultVo<OrderDto> orderCancel(@RequestParam(value = "openid") String openid, @RequestParam(value = "orderid") String orderid)
    {
        buyerService.cancelOrder(orderid,openid);
        log.info("opeid{}",openid);
        return ResultVoUtils.success();
    }
}
